import { b2Contact, b2ContactImpulse, b2ContactListener, b2Manifold, b2ParticleBodyContact, b2ParticleContact, b2ParticleSystem, b2Shape } from "@flyover/box2d";
import { RigidBody } from "../../behaviours/RigidBody";
import { IntroductionPrefabBinding } from "../../bindings/IntroductionPrefabBinding";
import { MainRolePrefabBinding } from "../../bindings/MainRolePrefabBinding";
import { Scene1PrefabBinding } from "../../bindings/Scene1PrefabBinding";
import { Scene2PrefabBinding } from "../../bindings/Scene2PrefabBinding";
import { StartPrefabBinding } from "../../bindings/StartPrefabBinding";
import { getGameObjectById } from "../../engine";
import { PhysicsSystem } from "../../PhysicsSystem";
import { AnimationRenderer } from "../AnimationRenderer";
import { System } from "./System";

export class GamePlaySystem extends System implements b2ContactListener {


    public BeginContact(contact: b2Contact<b2Shape, b2Shape>): void {
        const body1 = contact.GetFixtureA().GetBody().GetUserData();
        const body2 = contact.GetFixtureB().GetBody().GetUserData();
        // const deadscene = this.gameEngine.createPrefab2('./assets/prefabs/Dead3.yaml');
        console.log(body1, body2);
        if ((body1.id === 'mainRole' && body2.id === 'TestBox1') || (body1.id === 'TestBox1' && body2.id === 'mainRole')) {
            // const scene1Binding = new Scene1PrefabBinding();
            // const scene1 = this.gameEngine.createPrefab(scene1Binding);
            const parrent = this.rootGameObject;
            const scene1 = getGameObjectById('Scene1');
            parrent.removeChild(scene1);
            console.log('切换场景！！！');
            const scene2 = this.gameEngine.createPrefab2('./assets/prefabs/Scene2.yaml');
            parrent.addChild(scene2);
        }
        if ((body1.id === 'mainRole' && body2.id === 'TestBox2') || (body1.id === 'TestBox2' && body2.id === 'mainRole')) {
            // const scene1Binding = new Scene1PrefabBinding();
            // const scene1 = this.gameEngine.createPrefab(scene1Binding);
            const parrent = this.rootGameObject;
            const scene2 = getGameObjectById('Scene2');
            parrent.removeChild(scene2);
            console.log('切换场景！！！');
            const scene3 = this.gameEngine.createPrefab2('./assets/prefabs/Scene3.yaml');
            parrent.addChild(scene3);
        }
        if ((body1.id === 'mainRole' && body2.id === 'TestBox3') || (body1.id === 'TestBox3' && body2.id === 'mainRole')) {
            // const scene1Binding = new Scene1PrefabBinding();
            // const scene1 = this.gameEngine.createPrefab(scene1Binding);
            const parrent = this.rootGameObject;
            const scene3 = getGameObjectById('Scene3');
            parrent.removeChild(scene3);
            console.log('切换场景！！！');
            const scene4 = this.gameEngine.createPrefab2('./assets/prefabs/Scene4.yaml');
            parrent.addChild(scene4);
        }
        if ((body1.id === 'mainRole' && body2.id === 'TestBox4') || (body1.id === 'TestBox4' && body2.id === 'mainRole')) {
            // const scene1Binding = new Scene1PrefabBinding();
            // const scene1 = this.gameEngine.createPrefab(scene1Binding);
            const parrent = this.rootGameObject;
            const scene4 = getGameObjectById('Scene4');
            parrent.removeChild(scene4);
            console.log('切换场景！！！');
            const scene5 = this.gameEngine.createPrefab2('./assets/prefabs/Scene5.yaml');
            parrent.addChild(scene5);
        }
        if ((body1.id === 'mainRole' && body2.id === 'TestBox5') || (body1.id === 'TestBox5' && body2.id === 'mainRole')) {
            // const scene1Binding = new Scene1PrefabBinding();
            // const scene1 = this.gameEngine.createPrefab(scene1Binding);
            const parrent = this.rootGameObject;
            const scene5 = getGameObjectById('Scene5');
            parrent.removeChild(scene5);
            console.log('切换场景！！！');
            const overscene1 = this.gameEngine.createPrefab2('./assets/prefabs/Ending1.yaml');
            parrent.addChild(overscene1);
            setTimeout(() => {
                parrent.removeChild(overscene1);
                const overscene2 = this.gameEngine.createPrefab2('./assets/prefabs/Ending2.yaml');
                parrent.addChild(overscene2);
            }, 2000)
        }
        //     if((body1.id === 'mainRole' && body2.id === 'trap2') || (body1.id === 'trap2' && body2.id === 'mainRole')){
        //         // body1.getBehaviour(MainRolePrefabBinding).action = 'die'
        //         const parrent = this.rootGameObject;
        //         const scene1 = getGameObjectById('Scene1');
        //         const DeadScene = this.gameEngine.createPrefab2('./assets/prefabs/Dead1.yaml');

        //         parrent.addChild(DeadScene);        
        //         parrent.removeChild(scene1);

        //         const button5 = getGameObjectById('Button5');

        //             button5.onClick = (event) => {
        //                 const dead = getGameObjectById('Dead1')
        //                 const scene11 = this.gameEngine.createPrefab2('./assets/prefabs/Scene1.yaml');
        //                 parrent.removeChild(dead);
        //                 parrent.addChild(scene11);
        //             }
        //     }
        //     if((body1.id === 'mainRole' && body2.id === 'Trap1') || (body1.id === 'Trap1' && body2.id === 'mainRole')){
        //         // body1.getBehaviour(MainRolePrefabBinding).action = 'die'
        //         const parrent = this.rootGameObject;
        //         const scene2 = getGameObjectById('Scene2');
        //         const DeadScene = this.gameEngine.createPrefab2('./assets/prefabs/Dead2.yaml');

        //         parrent.addChild(DeadScene);        
        //         parrent.removeChild(scene2);

        //         const button5 = getGameObjectById('Button5');

        //             button5.onClick = (event) => {
        //                 const dead = getGameObjectById('Dead2')
        //                 const scene11 = this.gameEngine.createPrefab2('./assets/prefabs/Scene2.yaml');
        //                 parrent.removeChild(dead);
        //                 parrent.addChild(scene11);
        //             }
        //     }
        //     if((body1.id === 'mainRole' && body2.id === 'mushroom') || (body1.id === 'mushroom' && body2.id === 'mainRole')){
        //         // body1.getBehaviour(MainRolePrefabBinding).action = 'die'
        //         const parrent = this.rootGameObject;
        //         const scene3 = getGameObjectById('Scene3');
        //         const DeadScene = this.gameEngine.createPrefab2('./assets/prefabs/Dead3.yaml');

        //         parrent.addChild(DeadScene);        
        //         parrent.removeChild(scene3);

        //         const button5 = getGameObjectById('Button5');

        //             button5.onClick = (event) => {
        //                 const dead = getGameObjectById('Dead3')
        //                 const scene11 = this.gameEngine.createPrefab2('./assets/prefabs/Scene3.yaml');
        //                 parrent.removeChild(dead);
        //                 parrent.addChild(scene11);
        //             }
        //     }
        //     if((body1.id === 'mainRole' && body2.id === 'gear') || (body1.id === 'gear' && body2.id === 'mainRole')){
        //         // body1.getBehaviour(MainRolePrefabBinding).action = 'die'
        //         const parrent = this.rootGameObject;
        //         const scene4 = getGameObjectById('Scene4');
        //         const DeadScene = this.gameEngine.createPrefab2('./assets/prefabs/Dead4.yaml');

        //         parrent.addChild(DeadScene);        
        //         parrent.removeChild(scene4);

        //         const button5 = getGameObjectById('Button5');

        //             button5.onClick = (event) => {
        //                 const dead = getGameObjectById('Dead4')
        //                 const scene11 = this.gameEngine.createPrefab2('./assets/prefabs/Scene4.yaml');
        //                 parrent.removeChild(dead);
        //                 parrent.addChild(scene11);
        //             }
        //     }
        //     if((body1.id === 'mainRole' && body2.id === 'trap5') || (body1.id === 'trap5' && body2.id === 'mainRole')){
        //         // body1.getBehaviour(MainRolePrefabBinding).action = 'die'
        //         const parrent = this.rootGameObject;
        //         const scene5 = getGameObjectById('Scene5');
        //         const DeadScene = this.gameEngine.createPrefab2('./assets/prefabs/Dead5.yaml');

        //         parrent.addChild(DeadScene);        
        //         parrent.removeChild(scene5);

        //         const button5 = getGameObjectById('Button5');

        //             button5.onClick = (event) => {
        //                 const dead = getGameObjectById('Dead5')
        //                 const scene11 = this.gameEngine.createPrefab2('./assets/prefabs/Scene5.yaml');
        //                 parrent.removeChild(dead);
        //                 parrent.addChild(scene11);
        //             }
        //     }
    }


    onStart(): void {
        console.log('开始游戏')

        this.gameEngine.getSystem(PhysicsSystem).SetContactListener(this);


        const scene = getGameObjectById('AllScene');

        const StartScene = this.gameEngine.createPrefab2('./assets/prefabs/Start.yaml');
        scene.addChild(StartScene);

        const IntroductionScene = this.gameEngine.createPrefab2('./assets/prefabs/Introduction.yaml');
        const DeadScene = this.gameEngine.createPrefab2('./assets/prefabs/Dead3.yaml');
        // const Scene2 = this.gameEngine.createPrefab2('./assets/prefabs/Scene2.yaml');
        // const deadscene = this.gameEngine.createPrefab2('./assets/prefabs/Dead3.yaml');

        const button = getGameObjectById('Button1');
        const button3 = getGameObjectById('Button3');
        const button4 = getGameObjectById('Button4');
        // const trap = getGameObjectById('trap2666');
        // const rigid = trap.getBehaviour(RigidBody);
        // if(rigid.y <= 60){
        //     rigid.b2RigidBody.SetLinearVelocity({ x: 0, y: -1 })
        // }
        // if(rigid.y == 340){
        //     rigid.b2RigidBody.SetLinearVelocity({ x: 0, y: 1 })
        // }

        // 开始界面Start按钮
        button.onClick = (event) => {
            scene.addChild(IntroductionScene)
        }

        // 介绍界面I'm sure按钮
        button3.onClick = (event) => {
            scene.removeChild(StartScene);
            scene.removeChild(IntroductionScene);
            const Scene1 = this.gameEngine.createPrefab2('./assets/prefabs/Scene1.yaml');
            scene.addChild(Scene1);
        }

        // 介绍界面I'm not sure按钮
        button4.onClick = (event) => {
            scene.addChild(StartScene);
        }

        this.play();

    }

    public play() {
        let audio = new Audio("assets/music/qingtian.mp3");
        let AudioContext = new (window.AudioContext);
        let source = AudioContext.createMediaElementSource(audio)
        let analyser = AudioContext.createAnalyser();
        source.connect(analyser);
        analyser.connect(AudioContext.destination);
        audio.currentTime = 0;
        audio.play();
    }
    onUpdate(): void {

    }




    public EndContact(contact: b2Contact<b2Shape, b2Shape>): void {



        //碰到尖刺id为trap2的，死亡动画，然后移走
        // const mainRole = contact.GetFixtureA().GetBody().GetUserData();
        // const trap = contact.GetFixtureB().GetBody().GetUserData();
        // if (mainRole.id == 'mainRole' && trap.id == 'trap2') {
        //     mainRole.children[0].getBehaviour(AnimationRenderer).action = 'die';
        //     // mainRole.parent.removeChild(mainRole);
        // }
    }
    public PreSolve(contact: b2Contact<b2Shape, b2Shape>, oldManifold: b2Manifold): void {
    }
    public PostSolve(contact: b2Contact<b2Shape, b2Shape>, impulse: b2ContactImpulse): void {
    }




    public BeginContactFixtureParticle(system: b2ParticleSystem, contact: b2ParticleBodyContact): void {
    }
    public EndContactFixtureParticle(system: b2ParticleSystem, contact: b2ParticleBodyContact): void {
    }
    public BeginContactParticleParticle(system: b2ParticleSystem, contact: b2ParticleContact): void {
    }
    public EndContactParticleParticle(system: b2ParticleSystem, contact: b2ParticleContact): void {
    }
}